package com.stars.services.impl.system;
import com.stars.entity.system.SysOrgEntity;
import com.stars.services.system.SysOrgConfigServices;
import com.stars.dao.mapper.system.SysOrgMapper;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.stars.common.ObjectCopyUtils;
import com.stars.common.base.BaseConstant;
import com.stars.common.base.ResultMsg;
import com.stars.common.tree.TreeJson;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.stars.services.impl.BaseConfigTreeServiceImpl;
/**
 * @Title: 公司组织架构 业务逻辑
 * @author dj
 * @date 2019-10-12 16:15:41
 * @version V1.0
 *
 */
@Service
public class SysOrgConfigServicesImpl  extends BaseConfigTreeServiceImpl<SysOrgMapper , SysOrgEntity> implements SysOrgConfigServices {

    @Autowired
    private SysOrgMapper sysOrgMapper;

    /**
    * 添加菜单数据
    * @param treeJson
    * @param map
    * @throws Exception
    */
    public void add(TreeJson treeJson, Map<String,String> map) throws Exception{
        Gson gson = new Gson();
        if(StringUtils.isBlank(treeJson.getAddJson())){
           return;
        }
        List<SysOrgEntity> addList= gson.fromJson(treeJson.getAddJson(), new TypeToken<List<SysOrgEntity>>() {}.getType());
        for (SysOrgEntity add : addList) {
           //不包含Ext情况不往下走
           if(add.getPid().indexOf("Ext") ==-1){
             SysOrgEntity entity = toAddEntity(treeJson, add);
             insert(entity);
             map.put(add.getId(),entity.getId());
             addParentId(add,entity.getId(),addList,treeJson,map);
           }
        }
    }
      /**
      * 递归添加树形
      * @param addList
      * @param treeJson
      */
      private void addParentId(SysOrgEntity root,String parentId,List<SysOrgEntity> addList,TreeJson treeJson, Map<String,String> map) throws Exception{
          for(SysOrgEntity c:addList){
             if(root.getId().equals(c.getPid())){
               SysOrgEntity cc = toAddEntity(treeJson, c);
               cc.setPid(parentId);
               insert(cc);
               map.put(c.getId(),cc.getId());
               addParentId(c,cc.getId(),addList,treeJson,map);
             }
          }
          }
      private SysOrgEntity toAddEntity(TreeJson treeJson, SysOrgEntity add) {
          SysOrgEntity entity = new SysOrgEntity();
           ObjectCopyUtils.copyPropertiesIf(add,entity, BaseConstant.ADD);
            ObjectCopyUtils.copyPropertiesIf(treeJson,entity, BaseConstant.ADD_ORG);
           return entity;
       }
       /**
       * 更新菜单数据
       * @param treeJson
       * @param map
       * @throws Exception
       */
       public void update(TreeJson treeJson,Map<String,String> map) throws Exception{
           Gson gson = new Gson();
           if(StringUtils.isBlank(treeJson.getUpdateJson())){
               return;
           }
           List<SysOrgEntity> updateList= gson.fromJson(treeJson.getUpdateJson(), new TypeToken<List<SysOrgEntity>>() {}.getType());
             for (SysOrgEntity  updateTree : updateList) {
                 SysOrgEntity  entity = sysOrgMapper.selectByPrimaryKey(updateTree.getId());
                 if(null == entity){
                     return;
                 }
                 ObjectCopyUtils.copyPropertiesIf(updateTree,entity,BaseConstant.EXCLUDES_UPDATE_ORG);
                 entity.setUpdateInfo(treeJson);
                 //如果包含ext ，不往下走
                 if(StringUtils.isNotBlank(updateTree.getPid()) && updateTree.getPid().indexOf("Ext") ==0){
                    //如果包含
                    entity.setPid(map.get(updateTree.getPid()));
                 }
                 updateSelectiveById(entity);
             }
         }


}